//
//  MLSatusCell.swift
//  MLWeiBo
//
//  Created by wenxing on 2017/1/9.
//  Copyright © 2017年 Se7venX. All rights reserved.
//

import UIKit
import SDWebImage


let screenW:CGFloat = UIScreen.main.bounds.width
let screenH:CGFloat = UIScreen.main.bounds.height
let margin:CGFloat = 8


class MLSatusCell: UITableViewCell {
    
    //用户头像
    @IBOutlet weak var iconView: UIImageView!
    //用户名称
    @IBOutlet weak var nameLabel: UILabel!
    //用户等级
    @IBOutlet weak var mbRankView: UIImageView!
    //微博时间
    @IBOutlet weak var timeLabel: UILabel!
    //微博来源
    @IBOutlet weak var sourceLabel: UILabel!
    //微博正文
    @IBOutlet weak var contentLabel: UILabel!
    //微博用户认证类型
    @IBOutlet weak var avatarView: UIImageView!
    //配图视图
    @IBOutlet weak var pictureView: MLPictureView!
    @IBOutlet weak var pictureLayout: UICollectionViewFlowLayout!
    
    @IBOutlet weak var pictureHeightCons: NSLayoutConstraint!
    
    @IBOutlet weak var pictureWidthCons: NSLayoutConstraint!
    
    /*
     * 转发特殊的
     */
    @IBOutlet weak var retweetTextLab: UILabel!
    
    @IBOutlet weak var pictureTopCons: NSLayoutConstraint!
    
    @IBOutlet weak var toolBar: UIView!
    
    var statusViewModel:MLStatusViewModel? {
        
        didSet{
            
            timeLabel.text = statusViewModel?.timeTex
            sourceLabel.text = statusViewModel?.sourceText
            
//            pictureView.backgroundColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
            
            pictureTopCons.constant = CGFloat(statusViewModel?.all_pic_urls?.count ?? 0)
            
            pictureView.pictureInfos = statusViewModel?.all_pic_urls

            let sizeSet = statusViewModel?.collectionViewSize
            
//            if pictureView.pictureInfos?.count==1 {
//               
//                pictureWidthCons.constant = sizeSet?.1.width ?? 0
//                pictureHeightCons.constant = sizeSet?.1.height ?? 0
// 
//            }else{
            
                pictureWidthCons.constant = sizeSet?.0.width ?? 0
                pictureHeightCons.constant = sizeSet?.0.height ?? 0
                
//            }
            
            pictureLayout.itemSize = (sizeSet?.1)!
            
//            pictureHeightCons.constant = statusViewModel?.collectionViewSize.height ?? 0
//            pictureWidthCons.constant = statusViewModel?.collectionViewSize.width ?? 0
            
            // 8.0 之后的bug
            pictureView.layoutIfNeeded()
            
            iconView.sd_setImage(with: statusViewModel?.iconURL, placeholderImage: #imageLiteral(resourceName: "avatar_default"))
            nameLabel.text = statusViewModel?.status?.user?.name
            
            mbRankView.image = statusViewModel?.mbankImage
            
            avatarView.image = statusViewModel?.verifitedImage
            
            contentLabel.text = statusViewModel?.status?.text
            
            /*
             *  转发
             */
            retweetTextLab?.text = statusViewModel?.status?.retweeted_status?.text
            
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        contentLabel.numberOfLines = 0
        
        contentLabel.preferredMaxLayoutWidth = screenW - CGFloat(2)*margin
        
        
        
        
        //配置collectionView
        pictureLayout.itemSize = CGSize(width: imageWidth, height: imageWidth)
        pictureLayout.minimumLineSpacing = 2
        pictureLayout.minimumInteritemSpacing = 2
        
        /*
         * 转发
         */
        retweetTextLab?.numberOfLines = 0
        
        retweetTextLab?.preferredMaxLayoutWidth = screenW - CGFloat(4)*margin
 
    }

}
